// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.properties.controllers;

import org.dengues.core.CorePlugin;
import org.dengues.core.components.ENodeParameterName;
import org.dengues.core.components.IXMLComponent;
import org.dengues.core.components.NodePropertyParameter;
import org.dengues.core.metadata.MetadataTable;
import org.dengues.core.property.NodeParameterUtils;
import org.dengues.designer.ui.i18n.Messages;
import org.dengues.designer.ui.metadata.MetadataDialog;
import org.dengues.designer.ui.process.models.CompNode;
import org.dengues.designer.ui.properties.PropertyTabDynamicSection;
import org.dengues.designer.ui.properties.cmd.AbstractPropertyCommand;
import org.dengues.designer.ui.properties.cmd.PropertyChangeMetadataCommand;
import org.dengues.designer.ui.properties.cmd.PropertyChangeTextCommand;
import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.jface.fieldassist.FieldDecoration;
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * @since 2007-9-27 下午09:54:31
 */
public class MetadataPropertyController extends AbstractPropertyController {

    /**
     * Qiang.Zhang.Adolf@gmail.com MetadataPropertyController constructor comment.
     * 
     * @param dps
     */
    public MetadataPropertyController(PropertyTabDynamicSection dps) {
        super(dps);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.properties.controllers.AbstractPropertyController#createControl(org.eclipse.swt.widgets.Composite,
     * org.dengues.core.components.NodePropertyParameter, org.eclipse.swt.widgets.Control)
     */
    @Override
    public Control createControl(final Composite composite, final NodePropertyParameter param, Control lastControl) {
        Composite subcomposite = new Composite(composite, SWT.NONE);
        GridLayout gridLayout = new GridLayout(4, false);
        gridLayout.marginBottom = 0;
        gridLayout.marginHeight = 0;
        gridLayout.marginLeft = 0;
        gridLayout.marginRight = 0;
        gridLayout.marginTop = 0;
        gridLayout.marginWidth = 0;
        subcomposite.setLayout(gridLayout);
        subcomposite.setLayoutData(new GridData(GridData.FILL_BOTH));
        subcomposite.setBackground(composite.getBackground());
        CLabel cLabel = getWidgetFactory().createCLabel(subcomposite, param.getDisplayName());
        GridData gridData = new GridData();
        gridData.widthHint = STANDARD_LABEL;
        cLabel.setLayoutData(gridData);
        ControlDecoration controlDecoration = new ControlDecoration(getWidgetFactory().createCCombo(subcomposite, SWT.BORDER),
                SWT.RIGHT | SWT.TOP);
        if (param.isRequired()) {
            FieldDecoration fieldDecoration = FieldDecorationRegistry.getDefault().getFieldDecoration(
                    FieldDecorationRegistry.DEC_REQUIRED);
            controlDecoration.setImage(fieldDecoration.getImage());
            controlDecoration.setDescriptionText(fieldDecoration.getDescription());
        }
        final CCombo combo = (CCombo) controlDecoration.getControl();
        GridData data = new GridData();
        data.heightHint = STANDARD_HEIGHT;
        data.horizontalIndent = FieldDecorationRegistry.getDefault().getMaximumDecorationWidth();
        combo.setLayoutData(data);
        combo.setData(PARAMETER_NAME, param.getName());
        String[] displayItems = getDisplayItems(param);
        combo.setItems(displayItems);
        final CompNode compNode2 = (CompNode) dps.getElem();
        if (param.getValue() instanceof String) {
            combo.setText((String) param.getValue());
        } else if (displayItems.length > 0) {
            combo.select(0);
            compNode2.getMetadataList().clear();
            compNode2.getMetadataList().add(NodeParameterUtils.getWarehouseTable(combo.getText()));
            PropertyChangeTextCommand command = new PropertyChangeTextCommand(dps.getElem(), param.getName(), combo.getText());
            getCommandStack().execute(command);
        }
        combo.setBackground(composite.getBackground());
        combo.setEditable(false);
        combo.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                CCombo cb1 = (CCombo) e.getSource();
                AbstractPropertyCommand command = new PropertyChangeTextCommand(dps.getElem(), param.getName(), cb1.getText());
                getCommandStack().execute(command);
                if (param.getName().equals(ENodeParameterName.METADATA.getName())) {
                    command = new PropertyChangeMetadataCommand(compNode2, NodeParameterUtils.getWarehouseTable(cb1.getText()));
                    getCommandStack().execute(command);
                }
            }

        });
        Button button = getWidgetFactory().createButton(subcomposite, "...", SWT.PUSH);
        data = new GridData();
        data.heightHint = BUTTON_HEIGHT;
        data.widthHint = BUTTON_HEIGHT;
        button.setLayoutData(data);
        button.addSelectionListener(new SelectionAdapter() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
             */
            @Override
            public void widgetSelected(SelectionEvent e) {
                if (param.getName().equals(ENodeParameterName.CONNECTIVITY.getName())) {
                    CorePlugin.getDefault().getWarehousePluginService().getOpenPrefsDialog().run();
                } else if (param.getName().equals(ENodeParameterName.METADATA.getName())) {
                    MetadataDialog.TITLE = Messages.getString("MetadataPropertyController.schema"); //$NON-NLS-1$
                    MetadataTable metadataTable = compNode2.getMetadataList().get(0);
                    MetadataTable mtable = new MetadataTable();
                    if (metadataTable != null) {
                        mtable = metadataTable.clone();
                    }
                    MetadataDialog dialog = new MetadataDialog(composite.getShell(), mtable);
                    dialog.setCanModifyName(true);
                    int open = dialog.open();
                    if (open == MetadataDialog.OK) {
                        MetadataTable table = dialog.getMetadataTable().clone();
                        table.setTableName(((CompNode) dps.getElem()).getUniqueName());
                        if (!table.sameAs(mtable, true)) {
                            AbstractPropertyCommand command = new PropertyChangeMetadataCommand(compNode2, table);
                            getCommandStack().execute(command);
                            command = new PropertyChangeTextCommand(dps.getElem(), param.getName(), IXMLComponent.CUSTOMIZE);
                            getCommandStack().execute(command);
                            combo.setText(IXMLComponent.CUSTOMIZE);
                        }
                    }
                }
            }
        });
        return button;
    }
}
